
// 网络请求模块
// var  baseUrl='//172.16.2.9:8080/wx-api';  //khy
//var  baseUrl='//172.16.2.32:8080/wx-api';  //yd
//var  baseUrl='//172.16.2.52:8080';  //zb
var baseUrl='http://59.110.18.99:80';  //预发
var uploadApiUrl ="http://oss.leley.org:8085/";   //预发上传
var basePictureUrl = "http://img.leley.org/";     //预发

// var  baseUrl='http://47.93.158.251';   //线上
// var uploadApiUrl = "http://oss.leley.com/";  //正式环境
// var basePictureUrl = "http://img.leley.com/"; //正式环境

function HttpRequest() {
    var _this = this;
    _this.baseRequest = function (cb,opts) {
        cb = cb || function () {};
        var defaults={
            url : '',
            type : 'post',
            data : {
                token:localStorage.token ,
                userId:localStorage.userId
            },
            timeout : 20000,
            contentType : 'application/x-www-form-urlencoded;charset=utf-8',
            traditional:true,
            processData : true,
            crossDomain: true,
            complete : function (xhr, status) {
                $('body').css('visibility','visible');
                var result = xhr.responseJSON || xhr.responseText && JSON.parse(xhr.responseText);
                if(xhr.status == 200 ){
                    if(result.code=='1-001' || result.code == '5'){
                        layer.msg('登录失效了，请重新登录！正在跳转到登录页面...');
                        setTimeout(function(){
                            location.href = './login.html';
                        }, 1900);
                    }
                    cb(result);
                }else if(status == 'timeout'){
                    layer.closeAll('loading');
                    layer.msg('请求超时，请稍后重试');
                    cb(result);
                }else{
                    cb(result);
                }
            }
        };
        opts = $.extend(true,defaults,opts);
        $.ajax(opts);
    };
}
//工具模块
var $$ = {};
//时间格式化到天（2017-02-06）
$$.formatToDay = function (time) {
    return new Date(time + 8 *3600 *1000).toISOString().replace(/T.*$/,'');
};
//时间格式化到秒（2017-02-06 14:42:01）
$$.formatToSecond = function (time) {
    return new Date(time + 8 *3600 *1000).toISOString().replace(/T|\..*Z/g," ").trim();
};
//时间戳格式化
$$.getLocalTime=function(nS) {
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
/**
 * 获取根路径
 * @returns {string}
 */
$$.getRootPath = function(){
    return document.location.protocol +'//'+ window.location.host;
}


/**
 * 获取上一个月
 *
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
$$.getPreMonth = function(date) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var day = arr[2]; //获取当前日期的日
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中月的天数
    var year2 = year;
    var month2 = parseInt(month) - 1;
    if (month2 == 0) {
        year2 = parseInt(year2) - 1;
        month2 = 12;
    }
    var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if (day2 > days2) {
        day2 = days2;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = year2 + '-' + month2 + '-' + day2;
    return t2;
}
Date.prototype.Format = function(fmt)
{
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}